/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package deatax.display.miniboard;

import deatax.Player;
import deatax.display.BoardRenderer;
import deatax.display.Model;
import deatax.display.ModelLoader;
import deatax.display.Texture;
import deatax.display.Tween;
import deatax.display.TweenListener;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.opengl.GL2;

/**
 *
 * @author nrechten
 */
public class MiniMarker implements TweenListener {

    Tween position;
    Model m;
    Model king;

    Player player;
    int mbPosition = 0;
    int target = 0;

    MiniBoard miniboard;

    public boolean onTheMove = false;
    private ArrayList<RevolutionListener> listeners;
    
    public MiniBoardTile tile;
    

    public MiniMarker(Player player, MiniBoard miniboard) {
        this.miniboard = miniboard;
        this.player = player;
        listeners = new ArrayList<RevolutionListener> ();

//        position = new ProximityTween(miniboard.getMarkerPosition(0), miniboard.getPg());
        position = new Tween(miniboard.getMarkerPosition(0));
        position.setListener(this);
        onTweenFinished(position);

        try {
            m = ModelLoader.loadModel("markers/base.obj");
            m.setTexture(new Texture("markers/base.png"));
            
            king = ModelLoader.loadModel("markers/king.obj");
            king.setTexture(new Texture("markers/king.png"));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MiniMarker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void addRevolutionListener(RevolutionListener l) {
        listeners.add(l);
    }
    
    public void removeRevolutionListener(RevolutionListener l) {
        listeners.remove(l);
    }
    
    public void setTarget(int target) {
        this.target = target;
        onTweenFinished(position);
    }

    public int getPosition() {
        return mbPosition;
    }

    public void draw(GL2 gl){

        if(!onTheMove && target != 0){
            onTweenFinished(position);
        }

        position.step();
        position.translate(gl);
        float scale = 1.5f;
        gl.glScalef(scale,scale,scale);
        BoardRenderer.glColor(gl, player.getColor());
        if(m != null){
            m.draw(gl);
        }
        gl.glColor3f(1, 1, 1);
        if(miniboard.data.checkLOD(BoardRenderer.LOD_KING) && king != null){
            king.draw(gl);
        }
        scale = 1/scale;
        gl.glScalef(scale,scale,scale);
//        gl.glBegin(GL2.GL_LINES);
//        gl.glVertex2d(1, 0);
//        gl.glVertex2d(-1, 0);
//        gl.glVertex2d(0, 1);
//        gl.glVertex2d(0, -1);
//        gl.glEnd();
        position.itranslate(gl);
    }


    public void onTweenFinished(Tween t) {
        if(target != 0){
            onTheMove = true;
            if (target > 0) {
                target--;
                mbPosition++;
            } else {
                target++;
                mbPosition--;
            }
            if (mbPosition < 0) {
                mbPosition += miniboard.size();
            } else if (mbPosition >= miniboard.size()) {
                mbPosition -= miniboard.size();
                for (RevolutionListener rl : listeners) {
                    rl.revolution(player);
                }
            }
            moveMarkerToTile(mbPosition);
//            position.setTarget(miniboard.getMarkerPosition(mbPosition));
        } else {
            position.resetSpeed();
            onTheMove = false;
        }
    }

    public void moveMarkerToTile(int tile){
        if(getTile() != null)
            getTile().removeMarker(this);
        setTile(miniboard.getTile(tile));
        getTile().addMarker(this);
    }
    
    public void setPosition(float x, float y){
        position.setTarget(x,y,getTile().height-0.5f);
    }
    
    public void setTile(MiniBoardTile tile) {
        this.tile = tile;
    }

    public MiniBoardTile getTile() {
        return tile;
    }
    
    

}
